<?php


namespace app\admin\controller;

use app\admin\model\Book as BookModel;

class Book extends Base
{
    /***
     * 展示页面
     */
    public function lists()
    {
        return $this->fetch();
    }

    /***
     * layui 数据表格接口
     */
    public function get_book_list()
    {
        $book = new BookModel();
        //排除被删除的
//        $this->_condition['where']['deleted'] = 0;
//        $this->_condition['with'] = 'user';
        $data  = $book->get_data_list($this->_condition);
        for($i=0; $i < count($data); $i++){
            $data[$i]['nick_name']  = $data[$i]->user->nick_name;
        }
        $count = $book->get_data_count($this->_condition['where']);
//        echo $this->_condition['where'];
//        die();
        response_json('', $data, $count);
    }

    /***
     * 删除
     */
    public function del()
    {
        $this->check_authority();
        if (!isset($this->_param['id']) || !isset($this->_param['name']))
            $this->error('参数错误');
        $book = new BookModel();
        $res     = $book->del(['id' => ['in', $this->_param['id']]]);
        if (!$res) {
            $this->error('删除失败!');
        } else {
            //写入操作日志
            if (is_array($this->_param['name']))
                $this->_param['name'] = implode(',', $this->_param['name']);
            log_write('删除书籍:' . $this->_param['name'], $book->getLastSql());
            $this->success('删除成功!', 'lists');
        }

    }
}